<template>

  <div class="dialog-container">
    <el-dialog
      :close-on-click-modal="false"
      :title="'评价签字'"
      :visible="dialogVisible"
      width="30%"
      @open="initData"
      @close="closeDialog"
    >
      <el-form
        ref="form"
        :model="dataForm"
        class="dialog-form"
        label-width="110px"
        size="mini"
      >

        <el-row type="flex" justify="center">

          <el-col type="flex" justify="center">
            <el-form-item label="处置人员评价" type="flex" justify="center">
              <el-rate
                v-model="dataForm.disposalPersonnelEvaluation"
                show-score
                text-color="#ff9900"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="center">

          <el-col type="flex" justify="center">
            <el-form-item label="运维结果评价" type="flex" justify="center">
              <el-rate
                v-model="dataForm.overallEvaluation"
                show-score
                text-color="#ff9900"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="center">

          <el-col type="flex" justify="center">
            <el-form-item label="内容说明" type="flex" justify="center">
              <el-autocomplete
                v-model="dataForm.contentDescription"
                :fetch-suggestions="querySearch"
                placeholder="请输入内容"
                type="textarea"
                :autosize="{ minRows: 4, maxRows: 10}"
                style="width: 300px"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex" justify="center">

          <el-col type="flex" justify="center">
            <el-form-item label="签字" type="flex" justify="center">
              <el-image
                style="width: 300px; height: 100px"
                :src="signPath"

                fit="contain"
              />

            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button :loading="saveLoading" class="sure-btn" size="mini" type="primary" @click="handleCommit('1')">确定</el-button>
        <el-button size="mini" class="cancel-btn" @click="closeDialog">取消</el-button>
      </span>

    </el-dialog>

  </div>
</template>

<script>
import { getTextPrompt } from '@/utils/common'
import { editEvaluation } from '@/api/maintenanceManage'
import { staticImgUrl } from '@/url'

export default {
  name: 'ConfirmMaintenanceDialog',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    dialogType: {
      type: String,
      default: 'detail'
    },
    obj: {
      type: Object,
      default: () => {
      }
    }
  },
  data() {
    return {
      saveLoading: false,
      dataForm: {
        overallEvaluation: null,
        disposalPersonnelEvaluation: null,
        contentDescription: null,
        signatureImagePath: null
      },
      restaurants: [],
      signPath: null
    }
  },
  created() {
    /*    let maintenanceStatussTemp=getDict('e016')*/
    /*    this.maintenanceStatuss = getDict('e016')*/
    /*    for (let i = 0; i < maintenanceStatussTemp.length; i++) {
      if(maintenanceStatussTemp[i].code!='e016_01' && maintenanceStatussTemp[i].code!='e016_02'){
        this.maintenanceStatuss.push(maintenanceStatussTemp[i])
      }
    }*/

  },
  methods: {
    querySearch(queryString, cb) {
      const restaurants = this.restaurants
      const results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter(queryString) {
      return (restaurant) => {
        return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
      }
    },

    initData() {
      this.restaurants = getTextPrompt('e061_02')
      this.signPath = staticImgUrl + this.obj.loginInfo.signPath
      this.dataForm.signatureImagePath = this.obj.loginInfo.signPath
      this.saveLoading = false
      this.dataForm.overallEvaluation = 0
      this.dataForm.disposalPersonnelEvaluation = 0
      this.dataForm.contentDescription = null
      // this.dataForm.signatureImagePath = null
    },
    /**
     * flag 记录是否评价，0否，1是
     */
    handleCommit(flag) {
      if (this.obj.doOp === '1') { // 1、说明是只评价传递过来的；空说明是确认按钮来的
        if (flag === '0') {
          this.$emit('closeDialog')
          return false
        }
      }

      this.saveLoading = true
      const data = []
      for (const argumentsKey in this.obj.ids) {
        data.push({ 'maintenanceId': this.obj.ids[argumentsKey],
          'overallEvaluation': this.dataForm.overallEvaluation,
          'disposalPersonnelEvaluation': this.dataForm.disposalPersonnelEvaluation,
          'contentDescription': this.dataForm.contentDescription,
          'signatureImagePath': this.dataForm.signatureImagePath,
          'op': flag
        })// 记录是否评价，0否，1是
      }
      if (this.dataForm.disposalPersonnelEvaluation != 0 && this.dataForm.overallEvaluation != 0) {
        editEvaluation(data).then(() => {
          if (flag === '1') {
            this.$message.success('评价成功')
          }
          this.$emit('closeDialog')
          this.$emit('flushPage')
          this.saveLoading = false
        }).catch(() => {
          this.saveLoading = false
        })
      }else{
        this.$message.success('请进行评分')
        this.saveLoading = false
      }
    },
    closeDialog() {
      this.handleCommit('0')
      /*     this.clearData()*/
      this.$refs['form'].clearValidate()
    }

  }

}
</script>

<style lang="scss" scoped>

</style>
